<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { numberApi } from '@/api/controller';
  import xmUpload from "@/components/Upload/xm-upload.vue";
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    Options:[] as any[]
  })
  const form = reactive({
    user_id: '',
    type: '',
    card_id: '',
    card_name:'',
    img: [] as any[],
  })
  const formRules = {
    type: [
      { required: true, trigger: "blur", message: "认证类型不可为空" },
    ],
    card_name: [
      { required: true, trigger: "blur", message: "姓名不可为空" },
    ],
    card_id: [
      { required: true, trigger: "blur", message: "身份证不可为空" },
    ],
    img: [{ required: true, trigger: "blur", message: "凭证不可为空" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      const loading = Toastloading();
      await numberApi.setActiveCertify(form)
      loading.close()
      closeDialog()
      ToastSuccess('操作成功！')
      $emit('updateList')
    })
  }
  const openDialog = (query:any = {})=> {
    const { user_id='', Options } = query;
    form.user_id = user_id
    action.Options = Options
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    Object.assign(form,{
      user_id: '',
      type: '',
      card_id: '',
      card_name:'',
      img: [] as any[],
    })
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="false"
    :show-close="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    title="认证信息">
      <el-form
        ref="dataForm"
        :model="form"
        :rules="formRules"
      >
        <el-form-item label="认证类型" label-width="150px" prop="type">
          <el-select
            v-model="form.type"
            placeholder="请选择认证类型"
          >
            <el-option
              v-for="(item, key) of action.Options"
              :label="item.label"
              :value="item.value"
              :key="key"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" label-width="150px" prop="card_name">
          <el-input v-model="form.card_name" />
        </el-form-item>
        <el-form-item label="身份证号" label-width="150px" prop="card_id">
          <el-input v-model="form.card_id" />
        </el-form-item>
        <el-form-item
          v-model="form.img"
          label="凭证"
          label-width="150px"
          prop="img"
        >
          <xmUpload
            :limit="0"
            v-model:fileList="form.img"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>